<template>
    <el-card class="box-card">
        <div slot="header" class="card-heaer">
            <span style="line-height: 36px;">意外残疾理赔信息录入</span>
            <el-button type="primary" style="float:right" @click="up"  v-if="state!='查看'&&state!='结果'">提交</el-button>
        </div>
    
        <el-form ref="form" :model="obj" :rules.sync="modelrules" label-width="180px">
    
            <el-form-item label="意外发生时间:" prop="BILL_INJURE_TIME">
                <el-date-picker v-model="obj.BILL_INJURE_TIME" type="datetime" :disabled="disabled" placeholder="选择日期时间">
                </el-date-picker>
            </el-form-item>
            <el-form-item label="就医时间:" prop="BILL_INJURE_HTIME">
                <el-date-picker v-model="obj.BILL_INJURE_HTIME" type="datetime" :disabled="disabled" placeholder="选择日期时间">
                </el-date-picker>
            </el-form-item>
            <el-form-item label="被保险人所在地:" prop="BILL_INJURE_ADDRESS">
                <el-radio-group style="float:left; ">
                    <el-cascader :disabled="disabled" :options="address" v-model="obj.BILL_INJURE_ADDRESS">
                    </el-cascader>
                </el-radio-group>
    
            </el-form-item>
    
            <el-form-item label="详细地址:" prop="BILL_INJURE_ADDRESSDETAIL">
                <el-input v-model="obj.BILL_INJURE_ADDRESSDETAIL" :disabled="disabled" style="float:left; width:200px; padding-left:5px"></el-input>
            </el-form-item>
            <el-form-item label="详情描述:" prop="BILL_INJURE_DETAILS">
                <el-input type="textarea" v-model="obj.BILL_INJURE_DETAILS" :disabled="disabled"></el-input>
            </el-form-item>
            <el-form-item label="保险人就医过程描述:" prop="BILL_INJURE_PROCESS">
                <el-input type="textarea" v-model="obj.BILL_INJURE_PROCESS" :disabled="disabled"></el-input>
            </el-form-item>
            <el-form-item label="伤残鉴定时间:" prop="BILL_INJURE_AUTHTIME">
                <el-date-picker v-model="obj.BILL_INJURE_AUTHTIME" type="datetime" :disabled="disabled" placeholder="选择日期时间">
                </el-date-picker>
            </el-form-item>
            <el-form-item label="伤残鉴定等级:" prop="BILL_INJURE_AUTHLEVEL">
                <el-select v-model="obj.BILL_INJURE_AUTHLEVEL" placeholder="请选择">
                    <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="伤残等级详情描述:" prop="BILL_INJURE_AUTHREASON">
                <el-input type="textarea" v-model="obj.BILL_INJURE_AUTHREASON" :disabled="disabled"></el-input>
            </el-form-item>
            <el-form-item label="附件上传:" prop="BILL_INJURE_AUTHDOC" v-if="state!='理赔审核' && state!='查看'&& state!='结果'">
                <el-button type="primary" class="fileselect">
                    <input type="file" @change="upload" value="" id="fileup"> 上传附件
                </el-button>
            </el-form-item>
            <el-form-item label="附件下载:" v-if="state=='理赔审核'||state=='结果' ||state=='查看'">
                <el-button type="primary" class="fileselect" @click="download">
                    下载附件
                </el-button>
            </el-form-item>
            <el-form-item label="审核:" v-if="state=='理赔审核'">
                   <el-select placeholder="审核" v-model="obj.BILL_INJURE_STATE">
                    <el-option label="审核通过" value="需计算" ></el-option>
                    <el-option label="退回重新填写" value="需理赔修改"></el-option>
                    <el-option label="拒赔" value="理赔审核不通过"></el-option>
                </el-select>
            </el-form-item>
    
            <el-form-item label="不通过原因:" v-if="obj.BILL_INJURE_STATE=='理赔审核不通过'|| obj.BILL_INJURE_STATE=='需理赔修改' || state=='查看'">
                <el-input type="textarea" v-model="obj.BILL_INJURE_WHY" :disabled="state=='查看'||state=='修改'"></el-input>
            </el-form-item>
        </el-form>
    </el-card>
</template>
<script>
import getid from '@/js/getid';
import {
    mapState
} from 'vuex';
export default {
    data() {
        return {
            obj: this.$store.state.lipeiDuty.harmDisability,
            showwhy: false,
            hospatialList: [],
            issuccess: false, //是否通过,
            disabled: false,
            ischeck: false,           
            checkState: "",
            file: "",

            options: [{
                value: '一级',
                label: '一级'
            }, {
                value: '二级',
                label: '二级'
            }, {
                value: '三级',
                label: '三级'
            }, {
                value: '四级',
                label: '四级'
            }, {
                value: '五级',
                label: '五级'
            },
            {
                value: '六级',
                label: '六级'
            }, {
                value: '七级',
                label: '七级'
            }, {
                value: '八级',
                label: '八级'
            }, {
                value: '九级',
                label: '九级'
            }, {
                value: '十级',
                label: '十级'
            }],
        }
    },
    computed: mapState({
        // 报案人信息
        modelrules: state => state.lipeiDuty.harmDisabilityRules,
        address: state => state.select.address,
    }),
    props: {
        state: {
            type: String,
            default: "新增"
        }
    },
    methods: {
        up() {
            if (this.state == "新增") {
                this.add();
            } else {
                this.changer();
            }

        },
        changer() {
            var model = JSON.parse(JSON.stringify(this.obj));
            if(  model.BILL_INJURE_STATE ==""){
                 this.$notify({
                        title: '您还没有审核',
                        message: '您还没有审核',
                        type: 'warning'
                    });
                return;
            }
        
            if (this.state == "修改") {
                model.BILL_INJURE_STATE = "需理赔审核";
                model.BILL_INJURE_WHY="";
            }
            
            model.BILL_INJURE_ADDRESS = model.BILL_INJURE_ADDRESS.join(",");          
            model.BILL_INJURE_AUTHDOC = this.file;
            this.$http.put("BILL_INJURE", model).then(response => {
                var data = response.body;
                if (data.code == 1) {
                    this.$notify({
                        title: '提交成功',
                        message: '提交成功',
                        type: 'success'
                    });
                     this.$router.go(-1);
                } else {
                    this.$notify({
                        title: '提交失败',
                        message: '提交失败',
                        type: 'warning'
                    });
                }
            }, response => {
                this.loading = false;
                this.$notify.error({
                    title: '网络错误',
                    message: '网络错误'
                });
            });

           
        },
        add() {
            if (this.file == "") {
                this.$notify.error({
                    title: '数据不完整',
                    message: '请上传附件'
                });
                return;
            }
            var model = JSON.parse(JSON.stringify(this.obj));
            model.BILL_INJURE_ID = getid.getid();
            model.BILL_INJURE_ADDRESS = model.BILL_INJURE_ADDRESS.join(",");
            model.BILL_INJURE_STATE = "理赔审核";
            model.RI_INJURE_ID = this.$route.query["id"];
            model.BILL_INJURE_AUTHDOC = this.file;
          
            this.$http.post("BILL_INJURE", model).then(response => {
                var data = response.body;
                if (data.code == 1) {
                    this.$notify({
                        title: '添加成功',
                        message: '添加成功',
                        type: 'success'
                    });
                   this.$router.go(-1);
                } else {
                    this.$notify({
                        title:'提交失败',
                        message:data.msg,
                        type: 'warning'
                    });
                }
            }, response => {
                this.loading = false;
                this.$notify.error({
                    title: '网络错误',
                    message: '网络错误'
                });
            });
        },
        upload(event) {
            let file = event.target.files[0];
            // if (file.type != "application/x-zip-compressed") {
            //     this.$notify({
            //         title: '文件格式不正确',
            //         message: '仅支持zip格式文件',
            //         type: 'warning'
            //     });
            //     return;
            // }
            this.loading = true;
            if (file) {
                const formData = new FormData()
                formData.append('file', file)
                this.$http.post('ReportInfomation/file', formData, {
                    progress(event) {
                        //传递给父组件的progress方法
                       
                        // self.$emit('progress', parseFloat(event.loaded / event.total * 100), flag)
                    }
                }).then(response => {
                    var data = response.body;
                    this.loading = false;
                    if (data.code == 1) {

                        this.file = data.msg[0];
                        this.$notify({
                            title: '附件上传成功',
                            message: '附件上传成功',
                            type: 'success'
                        });
                        document.getElementById("fileup").value = "";

                    } else {
                        this.loding = false;
                        this.$notify.error({
                            title: '提交失败',
                            message: data.msg
                        });
                        document.getElementById("fileup").value = "";
                    }
                }, response => {
                    this.loading = false;
                    this.$notify.error({
                        title: '网络错误',
                        message: '网络错误'
                    });
                    this.loding = false;
                    document.getElementById("fileup").value = "";
                })


            }
        },
        download() {
            var url = this.$http.options.root;
            var base = url.substring(0, url.length - 3);
            window.open(base + "Upload/" + this.file);
        }
    },

    activated() {
        this.file = "";
        this.BILL_DIED_STATE = "";
        this.BILL_DIED_WHY = "";
        var url;
        if (this.state == "新增") {
            url = "Lipei/GetByType?id=" + this.$route.query["rid"] + "&type=意外残疾";
        } else {
            url = "BILL_INJURE/" + this.$route.query["id"];
        }
        this.$http.get(url).then(response => {
            var data = response.body;
            if (data.code == 1) {
                var address = data.result[0].BILL_INJURE_ADDRESS.split(",");
                address[2] = address[2] + "," + address[3];
                address[3] = null;
                data.result[0].BILL_INJURE_ADDRESS = address;
                if (this.state == "新增") {

                    this.obj.BILL_INJURE_TIME = data.result[0].BILL_INJURE_TIME,
                        this.obj.BILL_INJURE_HTIME = data.result[0].BILL_INJURE_HTIME,
                        this.obj.BILL_INJURE_ADDRESS = address,
                        this.obj.BILL_INJURE_DETAILS = data.result[0].BILL_INJURE_DETAILS,
                        this.obj.BILL_INJURE_ADDRESSDETAIL = data.result[0].BILL_INJURE_ADDRESSDETAIL;
                } else {
                     this.obj = data.result[0];
                    this.file = data.result[0].BILL_INJURE_AUTHDOC;
                    if (this.state == '理赔审核') {
                        this.obj.BILL_INJURE_STATE = "";
                    }
                }

            } else {
               
            }
        }, response => {
            this.$notify.error({
                title: '网络错误',
                message: '网络错误'
            });
        });


    }

}
</script>
